﻿// (c) Copyright slimCODE Software Inc. - www.slimcode.com
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using SlimCode.Windows.Controls.Mobile;

namespace SlimCode.SlimPasswords
{
  internal class UpDownOptionView : ItemView
  {
    private enum MouseDownPosition
    {
      Nowhere = 0,
      Toggle = 1,
      Decrement = 2,
      Increment = 3
    }

    private MouseDownPosition m_position = MouseDownPosition.Nowhere;

    public UpDownOptionView( int value, int minValue, int maxValue, string textPattern, string zeroPattern )
      : base( string.Empty, ContentAlignmentEx.MiddleLeft, 2 )
    {
      this.Value = value;
      this.MinValue = minValue;
      this.MaxValue = maxValue;
      this.TextPattern = textPattern ?? string.Empty;
      this.ZeroTextPattern = zeroPattern ?? string.Empty;

      this.UpdateText();
    }

    #region DecrementImage PROPERTY

    private Image m_decrementImage;

    private Image DecrementImage
    {
      get
      {
        if( m_decrementImage == null )
        {
          if( !this.ClientRectangle.IsEmpty )
          {
            m_decrementImage = ImageCache.GetDecrementImage( this.ClientRectangle.Height - 8 );
          }
        }

        return m_decrementImage;
      }
    }

    #endregion

    #region DecrementRectangle PROPERTY

    private Rectangle m_decrementRectangle = Rectangle.Empty;

    private Rectangle DecrementRectangle
    {
      get
      {
        if( m_decrementRectangle.IsEmpty )
        {
          if( !this.ClientRectangle.IsEmpty )
          {
            m_decrementRectangle = new Rectangle(
              this.ClientRectangle.Right - this.DecrementImage.Width - this.IncrementImage.Width - this.LineHeight,
              this.ClientRectangle.Top + ( this.ClientRectangle.Height - this.DecrementImage.Height ) / 2,
              this.DecrementImage.Width,
              this.DecrementImage.Height );
          }
        }

        return m_decrementRectangle;
      }
    }

    #endregion

    #region IncrementImage PROPERTY

    private Image m_incrementImage;

    private Image IncrementImage
    {
      get
      {
        if( m_incrementImage == null )
        {
          if( !this.ClientRectangle.IsEmpty )
          {
            m_incrementImage = ImageCache.GetIncrementImage( this.ClientRectangle.Height - 8 );
          }
        }

        return m_incrementImage;
      }
    }

    #endregion

    #region IncrementRectangle PROPERTY

    private Rectangle m_incrementRectangle = Rectangle.Empty;

    private Rectangle IncrementRectangle
    {
      get
      {
        if( m_incrementRectangle.IsEmpty )
        {
          if( !this.ClientRectangle.IsEmpty )
          {
            m_incrementRectangle = new Rectangle(
              this.ClientRectangle.Right - this.IncrementImage.Width - this.LineHeight / 2,
              this.ClientRectangle.Top + ( this.ClientRectangle.Height - this.IncrementImage.Height ) / 2,
              this.IncrementImage.Width,
              this.IncrementImage.Height );
          }
        }

        return m_incrementRectangle;
      }
    }

    #endregion

    #region MaxValue PROPERTY

    public int MaxValue { get; private set; }

    #endregion

    #region MinValue PROPERTY

    public int MinValue { get; private set; }

    #endregion

    #region TextPattern PROPERTY

    public string TextPattern { get; private set; }

    #endregion

    #region Value PROPERTY

    public int Value { get; private set; }

    #endregion

    #region ZeroTextPattern PROPERTY

    public string ZeroTextPattern { get; private set; }

    #endregion

    protected override void DrawTopLayer( SlimCode.Windows.Controls.Mobile.TemplatedPaintEventArgs e )
    {
      base.DrawTopLayer( e );

      e.Graphics.DrawImage(
        this.DecrementImage,
        this.DecrementRectangle,
        0, 0, this.DecrementImage.Width, this.DecrementImage.Height,
        GraphicsUnit.Pixel,
        e.Attributes );

      e.Graphics.DrawImage(
        this.IncrementImage,
        this.IncrementRectangle,
        0, 0, this.IncrementImage.Width, this.IncrementImage.Height,
        GraphicsUnit.Pixel,
        e.Attributes );
    }

    protected override void OnMouseDown( System.Windows.Forms.MouseEventArgs e )
    {
      base.OnMouseDown( e );

      if( this.DecrementRectangle.Contains( e.X, e.Y ) )
      {
        m_position = MouseDownPosition.Decrement;
      }
      else if( this.IncrementRectangle.Contains( e.X, e.Y ) )
      {
        m_position = MouseDownPosition.Increment;
      }
    }

    protected override void OnMouseMove( System.Windows.Forms.MouseEventArgs e )
    {
      m_decrementRectangle = Rectangle.Empty;
      m_incrementRectangle = Rectangle.Empty;

      base.OnMouseMove( e );
    }

    protected override void OnMouseUp( System.Windows.Forms.MouseEventArgs e )
    {
      base.OnMouseUp( e );

      if( this.DecrementRectangle.Contains( e.X, e.Y ) )
      {
        if( m_position == MouseDownPosition.Decrement )
        {
          if( this.Value > this.MinValue )
          {
            this.Value--;
            this.UpdateText();
          }
        }
      }
      else if( this.IncrementRectangle.Contains( e.X, e.Y ) )
      {
        if( m_position == MouseDownPosition.Increment )
        {
          if( this.Value < this.MaxValue )
          {
            this.Value++;
            this.UpdateText();
          }
        }
      }
    }

    private void UpdateText()
    {
      this.Text = string.Format(
        ( ( this.Value != 0 ) || ( string.IsNullOrEmpty( this.ZeroTextPattern ) ) )
          ? this.TextPattern : this.ZeroTextPattern,
        this.Value,
        ( this.Value == 1 ) ? string.Empty : "s" );
    }
  }
}
